<?php
class PersonalApp extends CommonApp
{

    //用户修改界面
    public function modify_passAct()
    {
        if ($_POST) {
            $userid = My::arr2var($_SESSION, 'member.memberid');
            $result = DB('homeuser')->clear()->where("id={$userid}")->find();
            if (md5(md5($_POST['old_password'])) != $result['password'])
                $this->ajaxReturn(300, array('message' => '原始密码输入有误！', 'name' => 'old_password'));
            if (!My::is_string($_POST['password']) || strlen($_POST['password']) < '6' || strlen($_POST['password']) > '20')
                $this->ajaxReturn(300, array('message' => '密码由6-20位的英文字母、数字和特殊符号组成', 'name' => 'password'));
            if ($_POST['password'] !== $_POST['password2'])
                $this->ajaxReturn(300, array('message' => '两次密码输入不一致！', 'name' => 'password2'));
            $_POST['password'] = md5(md5($_POST['password']));
            $_POST['editdate'] = strtotime(date("Y-m-d H:i:s"));
            $ok = DB('homeuser')->clear()->where("id={$userid}")->save($_POST);
            if ($ok >= 0) {
                unset($_SESSION['member']);
                $this->ajaxReturn(200, "密码修改成功,请重新登录！");
            } else        $this->ajaxReturn(300, "密码修改失败！");
        } else $this->display();

    }

    //个人设置
    public function settingAct()
    {
        $result = $this->checkLogin();
        if ($_POST) {
            $member = $_SESSION['member'];
            //用户名
            if (!My::is_string($_POST['username'], array('c')))
                $this->ajaxReturn(300, '真实姓名填写有误！');
            //学号
            if ($member['type'] == '2')
                if (strlen($_POST['params']['student_id']) != '11')
                    $this->ajaxReturn(300, '请填写正确的学号！');
            //院系
            if (isset($_POST['college']))
                if (empty($_POST['college']))
                    $this->ajaxReturn(300, '请选择院系！');
            //专业
            if ($member['type'] == '2')
                if (empty($_POST['class']))
                    $this->ajaxReturn(300, '请选择专业！');
            //手机号
            if (isset($_POST['number'])) {
                if (!My::is_tel($_POST['number']))
                    $this->ajaxReturn(300, '请填写正确的手机号！');
                $id = DB('homeuser')->where("number='{$_POST['number']}'")->getField('id');
                if ($id)
                    if ($id != $member['memberid'])
                        $this->ajaxReturn(300, '该手机号已注册，请重新填写！');
            }
            //邮箱
            if (!My::is_email($_POST['email']))
                $this->ajaxReturn(300, '邮箱填写有误！');
            $_POST['id'] = $member['memberid'];
            if ($result['type'] == '0')
                $ok = DB('user')->clear()->save($_POST);
            else {
                $_POST['editdate'] = strtotime(date("Y-m-d H:i:s"));
                $ok = DB('homeuser')->clear()->save($_POST);
            }
            if ($ok >= 0) {
                foreach ($_POST as $key => $value) {
                    if (isset($member[$key]))
                        $member[$key] = $_POST[$key];
                }
                unset($member['college_c']);
                unset($member['class_c']);
                $_SESSION['member'] = $member;
                $this->ajaxReturn(200, "修改成功");
            } else        $this->ajaxReturn(300, "修改失败");
        } else {
            $result['classes'] = M('admin.category')->id2submenu($_SESSION['member']['college']);//专业
            if (isset($result['params']))
                $result['params'] = My::decodeJson($result['params']);
            $this->assign('result', $result);
            $this->display();
        }
    }

    //图片验证
    public function imageAct()
    {
        $upload = new Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->allowExts = array('jpg', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath = PUB_DIR . '/share/images/uploads/'; // 设置附件上传（子）目录
        // 上传文件
        if (!$upload->upload()) {// 上传错误提示错误信息
            $this->ajaxReturn(300, $upload->getErrorMsg());
        } else {// 上传成功 获取上传文件信息
            $inf = $upload->getUploadFileInfo();
        }
        $this->ajaxReturn(200, 'share/images/uploads/' . $inf[0]['savename']);
    }

    /**
     * 清缓存文件
     */
    public function clearAct()
    {
        rm_dir(DATA_DIR . 'Compile');//编译缓存
        //rm_dir(DATA_DIR.'File');//文件缓存
        rm_dir(DATA_DIR . 'Field');//数据库模型缓存
        rm_dir(DATA_DIR . 'Cache');//cache缓存
        //rm_dir(DATA_DIR.'Log');//日志缓存
        //rm_dir(DATA_DIR.'Session');//Session文件
        $this->ajaxReturn(200, '已清空所有缓存文件！');

    }
}
